//
//  SystemConfig.m
//  Dingdong
//
//  Created by 程强 on 16/11/23.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "SystemConfig.h"

static SystemConfig *config = nil;

@implementation SystemConfig

+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        config = [[SystemConfig alloc]init];
    });
    return config;
}
- (instancetype)init
{
    if (self = [super init]) {
        _user = [[DDUserObj alloc]init];
        _cartManager = [DDCartManager new];
        _rootTabbar = (DDTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    }
    return self;
}
+ (NSString *)httpSignWithBody:(NSDictionary *)dic
{
    NSString *bodyStr = @"";
    if (dic) {
        bodyStr = [ToolKit JSONEncodeObj:dic];
    }
    NSString *token = [SystemConfig shareInstance].token?[SystemConfig shareInstance].token:@"";
    NSString *sign = @"";
    sign = [sign stringByAppendingString:bodyStr];
    sign = [sign stringByAppendingString:token];
    sign = [sign stringByAppendingString:AppVersion];
    sign = [sign stringByAppendingString:NowTimeStamp];
    NSString *md5Sign = [sign md5Encrypt];
//    NSLog(@"sign == %@  md5Sign == %@",sign,md5Sign);
    return md5Sign;
}
- (void)updateCartCountSueesss:(void(^)(NSString *count))block
{
    [[self.cartManager getCartNum]subscribeNext:^(id x) {
        [_rootTabbar setCartBadge:FormatString(@"%@", x)];
        if (block) {
            block(FormatString(@"%@", x));
        }
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)setCartCount:(NSInteger)count
{
    DDTabBarController *rootTabbar = (DDTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [rootTabbar setCartBadge:FormatString(@"%ld", count)];
}
@end
